package com.itheima.contoller;

import com.github.pagehelper.PageInfo;
import com.github.pagehelper.PageInterceptor;
import com.itheima.domain.Student;
import com.itheima.domain.User;
import com.itheima.mapper.JLMapper;
import com.itheima.service.Impl.JLServiceImpl;
import com.itheima.service.JLService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @param
 * @return
 */
@WebServlet("/user/*")
public class StudentServlet extends BeanServlet {
    //创建业务层对象复用
    public static JLService service=new JLServiceImpl();
    protected void login(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        User user=service.login(username,password);//调用业务层方法
        if(user!=null){//成功
            req.getRequestDispatcher(req.getContextPath()+"/pages/main.jsp").forward(req,resp);
        }else {
            req.getRequestDispatcher(req.getContextPath()+"/pages/login.jsp").forward(req,resp);
        }
    }
    protected void list(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
        //获取前端发送 当前页码
        String pageNumStr = req.getParameter("pageNum");
        int pageNum=1;//这个的意思是如果请求信息中不含页码信息，给个默认值
        if(pageNumStr!=null){
            pageNum=Integer.parseInt(pageNumStr);
        }
        //调用业务层方法
        List<Student> list=service.findAll(pageNum);
        //将查询的结果交给  PageInfo对象进行分页信息处理
        PageInfo<Student> pageInfo=new PageInfo<Student>(list);
        req.setAttribute("pageInfo",pageInfo);
        req.getRequestDispatcher(req.getContextPath()+"/pages/list.jsp").forward(req,resp);
    }

}
